<?php
header("Content-Type: text/html; charset=iso-8859-1");
ini_set("memory_limit","32M");
session_start();
require('include.php');
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
if(!$id){
	$id = (!empty($_SESSION['tmp_id'])) ? $_SESSION['tmp_id'] : rand();	
	$_SESSION['tmp_id'] = $id;
} 
$equipe = (!empty($_REQUEST['equipe'])) ? $_REQUEST['equipe'] : "";
$team = new equipe($equipe,'nom');
$equipe = $team->id_vbcll;
if($id){
	if(!empty($_REQUEST['submit'])){
		//définition des variables
		// error_log("$equipe");
		if(file_exists("photos/equipes/".$equipe."-tmp.jpg")) unlink("photos/equipes/".$equipe."-tmp.jpg");
		$base = "photos/equipes/";
		$dest = "photos/";
		$imgfile = $base.$equipe."-grand.jpg";
		list($width, $height) = getimagesize($imgfile);
		$ratio = ($width > 800) ? 800 / $width : 1;
		$dest_small = $dest."$id.jpeg";
		$cropStartX = $_POST['sx']*$ratio;
		$cropStartY = $_POST['sy']*$ratio;
		$cropW = $_POST['ex']*$ratio;
		$cropH = $_POST['ey']*$ratio;

		// création des deux images temporaires
		$origimg = imagecreatefromjpeg($imgfile);
		$cropimg = imagecreatetruecolor($cropW,$cropH);

		// résolution de l'image
		list($width, $height) = getimagesize($imgfile);

		// Crop
		imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

		// copie vers répertoire
		imagejpeg($cropimg, $dest_small, 100);

		// Début du redimensionnement
		$thumb = imagecreatetruecolor(100, 100);
		$source = imagecreatefromjpeg($dest_small);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, 100, 100, $cropW, $cropH);

		// copie vers répertoire
		imagejpeg($thumb, $dest_small, 100);
		// Fin du redimmensionnement
		echo "<html><head></head>\n";
		echo "<body onload = \"opener.window.document.getElementById('photo_$id').src = 'photos/$id.jpeg'; self.close();\">\n";
		echo "</body></html>\n";

	}
	else{
		$onload = 'onLoad="fnOnLoad();" onMouseDown="fnOnMouseDown();" onMouseUp="fnOnMouseUp();" ';
		echo "<html><head>\n";
		echo '<script src="javascripts/recadrer-image-javascript.js" type="text/javascript" ></script>'."\n";
		echo "<script type = 'text/javascript'>\n";
		echo "function writediv(mytext,mydiv){\n";
		echo "	if(el = opener.window.getElementById(mydiv)) el.innerHTML = mytext;\n";
		echo "}\n";
		echo "</script>\n";
		echo "<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />\n";
		echo "</head><body $onload>\n";
		// Le fichier
		$filename = "photos/equipes/".$equipe."-grand.jpg";
		list($width, $height) = getimagesize($filename);
		$percent = ($width > 800) ? 800 / $width : 1;
		
		// Calcul des nouvelles dimensions
		$new_width = $width * $percent;
		$new_height = $height * $percent;

		// Redimensionnement
		$image_p = imagecreatetruecolor($new_width, $new_height);
		$image = imagecreatefromjpeg($filename);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

		// Affichage
		imagejpeg($image_p,"photos/equipes/".$equipe."-tmp.jpg" , 100);
		$img = getimagesize("photos/equipes/".$equipe."-tmp.jpg");
		echo "<div id='conteneur' style='background: url(\"photos/equipes/".$equipe."-tmp.jpg\") no-repeat; width: $img[0]px; height: $img[1]px; margin: 0 auto;'>\n";

		echo "<div id='bloc_recadre' name='bloc_recadre' onMouseOver='fnOnMouseOver(\"bloc_recadre\", \"conteneur\");' onMouseOut='fnOnMouseOver();'></div> \n";
		echo "</div>\n";
		echo "<form action='crop_photo_admin.php' method='post'>\n";
		echo "<ol style = 'margin-left: 150px'><li>Positionnez le carr&eacute; de s&eacute;l&eacute;ction (en haut &agrave; gauche) sur le portrait</li>";
		echo "<li>Ajustez sa dimension en d&eacute;pla&ccedil;ant les coins du carr&eacute;</li>";
		echo "<li>cliquez sur &quot;D&eacute;couper&quot;</li></ol>\n";
		echo "<p class = 'center'><input type='hidden' id='sx' name='sx' value='' />\n";
		echo "<input type='hidden' id='sy' name='sy' value='' />\n";
		echo "<input type='hidden' id='ex' name='ex' value='' />\n";
		echo "<input type='hidden' id='ey' name='ey' value='' />\n";
		echo "<input type = 'hidden' name = 'id' value = '$id' />\n";
		echo "<input type = 'hidden' name = 'equipe' value = '$team->nom' />\n";		
		echo "<button type='submit' value='ok' name = 'submit'>D&eacute;couper</button></p>\n";
		echo "</form>\n";
		echo "</body></html>	\n";
	}
}
